`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date:    17:59:15 10/20/2012 
// Design Name: 
// Module Name:    MajorFSM 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
module MajorFSM(clk_i, reset_sync, startGame, increase_Incrash, ini_speed_x, ini_speed_y,
ball_x, ball_y, paddle_y_fromA, gameOver
    );
   input clk_i,reset_sync,startGame,increase_Incrash;
	input wire [5:0] ini_speed_x, ini_speed_y;
	input wire [9:0] paddle_y_fromA;
	output wire [9:0] ball_x, ball_y;
	output wire gameOver;	 
	wire startB, startC;
	reg  startB_n, startC_n; 
	assign startB = startB_n;
	assign startC = startC_n;		 
	localparam ini_dir_x = 0, ini_dir_y = 0;
		 
	wire [5:0] speed_x, speed_y;								
	wire dir_x, dir_y;
	
	wire [9:0] ball_x_fromC, ball_y_fromC;
	wire StateSignalFromB,StateSignalFromC;
	assign ball_x = ball_x_fromC;
	assign ball_y = ball_y_fromC;
	Minor_FSM_B ball_Crash (clk_i, reset_sync, startB, increase_Incrash, paddle_y_fromA, ini_speed_x, ini_speed_y,
											 ball_x_fromC, ball_y_fromC, 
											 speed_x, speed_y, dir_x, dir_y, gameOver, StateSignalFromB);
											 
	Minor_FSM_C ball_Update (clk_i, reset_sync, startC, speed_x, speed_y, dir_x, dir_y,
									 ball_x_fromC, ball_y_fromC,StateSignalFromC);
	//----------------------------------------------------------------------------------------------

	// Mayor Satate Machine
	//----------------------------------------------------------------------------------------------
   parameter state0 = 4'b0000; 
   parameter state1 = 4'b0001; 
   parameter state2 = 4'b0010; 
	reg [3:0] state;
	initial begin
		state=state0;
	end
   always@(posedge clk_i)
	
		if(reset_sync)
			state <= state0;
		else
		begin
			 case (state)
				state0 :
				begin
					if(startGame)
					begin 
						state <= state1;
						startB_n = 1;
					end
					else 
						state <= state0;
				end				
				state1 : 
				begin
					if (StateSignalFromB)
					begin
						startB_n = 0;
						startC_n = 1;
						state <= state2;
					end
					else
						state <= state1;
				end				
				state2 : 
				begin
					if (StateSignalFromC)
					begin
						startC_n = 0;
						startB_n = 1;
						state <= state1;
					end
					else
						state <= state2;
				end		
				default: begin  
					state <= state0;
				end
			endcase
		end

endmodule
